package com.model.dao;

//Generated Dec 12, 2013 5:03:38 PM by Hibernate Tools 4.0.0

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
* Home object for domain model class GooglemapsTable.
* @see com.model.dao.GooglemapsTable
* @author Hibernate Tools
*/
@Stateless
public class GooglemapsTableDAO {

	private static final Log log = LogFactory.getLog(GooglemapsTableDAO.class);

	Session session;
	Transaction tx;

	public void persist(GooglemapsTable transientInstance) {
		log.debug("persisting GooglemapsTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.persist(transientInstance);
			tx.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(GooglemapsTable persistentInstance) {
		log.debug("removing GooglemapsTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public GooglemapsTable merge(GooglemapsTable detachedInstance) {
		log.debug("merging GooglemapsTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			GooglemapsTable result = (GooglemapsTable) session
					.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public GooglemapsTable findById(int id) {
		log.debug("getting GooglemapsTable instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			GooglemapsTable instance = (GooglemapsTable) session.get(
					GooglemapsTable.class, id);
			tx.commit();
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<GooglemapsTable> findByAll() throws Exception {
		log.debug("getting GooglemapsTable instance ");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Query query = session.createQuery("select distinct gt from GooglemapsTable gt left Join fetch gt.userTable ut" +
																	  " left Join fetch ut.orderTables");
			List<GooglemapsTable> list = query.list();
			tx.commit();
			log.debug("findByAll successful");
			return list;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("findByAll failed", ex);
			throw ex;
		}
	}
	
	public static void main(String[] args) throws Exception {
		GooglemapsTableDAO dao=new GooglemapsTableDAO();
		dao.findByAll();
	}
}

